Introduction to the Command-line Interface (CLI)

aka Introduction to Linux Shell


Getting into the Terminal or Command Prompt

For Mac users:

  • Type "Command + Spacebar" or click on the Spotlight magnifying glass in the upper right-hand corner.
  • Type "Terminal"
  • Hit "Return"

You should see something like this:

For Windows users:

  • In the search bar in the lower left-hand corner, type "cmd"
  • Hit "Enter"

You should see somethign like this:

Finding out where you are:

Displays your location in the file system

pwd     # pwd (Linux)
chdir   # chdir (Windows)

In [6]:
# To run these commands in the Jupyter Notebook, preceed with a exclamation mark (!)

!pwd


/Users/squiresrb/Documents/version_control/BIOF309-2016-Fall/Week_03

Creates a directory

mkdir   # mkdir directory
mkdir   # mkdir directory

In [3]:
!mkdir test

In [4]:
!ls


Week03 - 01 - Introduction to the Command-line.ipynb
Week03 - 02 - Week 2 Homework Review.ipynb
Week03 - 03 - Working with Files.ipynb
Week03 - 04 - Homework.ipynb
test

Changes directories with a specified path (absolute path)

cd pathname    # cd /directory/directory
cd pathname    # cd C:/directory/directory

In [5]:
!cd test

Changes directories with a relative path

cd ..   # cd ..
cd..    # cd..

In [7]:
!pwd


/Users/squiresrb/Documents/version_control/BIOF309-2016-Fall/Week_03

In [ ]:

Absolute Paths versus Relative Paths

Absolute paths start at the root directory in Linux and at the Hard drive Letter in Windows:

/Users/username/Document/BIOF309/file.txt

C:/Users/username/Documents/BIOF309/file.txt

Relative paths are "relative" to the programs currentl location.

A file in the same directory can be prefaced with "./" in linux but Jupyter Notebook does not require it

.\file.txt    # Current directory

Working with files

Lists files

ls     # ls
dir    # dir

Create a file

nano   # nano hello.txt
dir >     # dir > hello.txt

Copies files

cp     # cp thisfile.txt /home/thisdirectory
copy   # copy thisfile.txt C:/Users/thisdirectory

In [ ]:

Moves files

mv    # mv thisfile.txt /home/thisdirectory
move  # move thisfile.txt C:/Users/thisdirectory

In [ ]:

Deletes files

rm    # rm thisfile.txt
del   # rm thisfile.txt

In [ ]:

Compares the contents of files

diff  # diff file1 file2
fc    # diff file1 file2

In [ ]:

Finds a string of text in a file

grep    # grep word or phrase thisfile.txt
find    # grep word or phrase thisfile.txt

In [ ]:

Views contents of a file

less    # less thisfile.txt
more    # less thisfile.txt

To get out type "q"


In [ ]:

Renames a file

mv     # mv thisfile.txt thatfile.txt
ren    # ren thisfile.txt thatfile.txt

In [ ]: